package model;

/**
 *
 * @author Dustin
 */
public class ComputerPlayer implements Player {
/**
     * The size of a hand of cards
     */
    public final static int HAND_SIZE = 3;

    private boolean isDealer;
    private String name;
    private Card[] hand = new Card[HAND_SIZE];

    public ComputerPlayer(String aName) {
        name = aName;
    }

    public boolean isDealer() {
        return isDealer;
    }

    public String getName() {
        return name;
    }

    public Card getCard(int index) {
        return hand[index];
    }

    public Card[] getHand() {
        return hand;
    }

    public void setDealer(boolean aValue) {
        isDealer = aValue;
    }

    public void setName(String aName) {
        name = aName;
    }

    public void setCard(int index, Card c) {
        hand[index] = c;
    }

    public void resetHand() {
        for(int i = 0; i < HAND_SIZE; i++) {
            hand[i] = null;
        }
    }

    public void swapCard(Table t, int ownIndex, int tableIndex) {
        Card temp = t.getCard(tableIndex);
        t.setCard(tableIndex, hand[ownIndex]);
        setCard(ownIndex, temp);
    }

    public void swapCards(Table t) {
        Card[] temp = getHand();
        for(int i = 0; i < temp.length; i++) {
            swapCard(t, i, i);
        }
    }

    public void doTurn(Table t) {
        double choice = Math.random();
        if(choice < 1/4) {
            pass(t);
        } else if(choice < 3/4) {
            swapCard(t, (int) Math.random() * HAND_SIZE,
                    (Math.random() < 0.5 ? (int) Math.random() * HAND_SIZE : Table.PLAYING_CARDS - 1));
        } else {
            swapCards(t);
        }
    }

    public void pass(Table t) {
        for(Player p : t.getPlayers()) {
            if(!p.equals(this)) {
                //set last turn
            }
        }
    }

    public void deal(Table t) {
        if(isDealer) {
            //get dealing start position
            int index = -1;
            for(int i = 0; i < Table.DEFAULT_SIZE && index == -1; i++) {
                if(t.getPlayer(i).equals(this)) {
                    index = ((i + 1) < Table.DEFAULT_SIZE) ? (i + 1) : 0;
                }
            }
            //deal cards
            Player[] players = t.getPlayers();
            for(int i = 0; i < HumanPlayer.HAND_SIZE; i++) {
                for(int j = 0; j < Table.DEFAULT_SIZE; j++) {
                    players[index].setCard(i, t.getDeck().pop());
                    //deal pot cards
                    if(isDealer) {
                        t.setCard(i, t.getDeck().pop());
                    }
                    index = index == Table.DEFAULT_SIZE - 1 ? 0 : index + 1;
                }
            }
            //deal blind card
            t.setCard(Table.PLAYING_CARDS - 1, t.getDeck().pop());
        }
    }
}
